//
//  ShardDetailViewController.m
//  Pianke
//
//  Created by BetaTheta on 16/1/22.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "ShardDetailViewController.h"
#import "PWNetWorking.h"

#import "ShardDetailCell.h"
#import "UIImageView+WebCache.h"


@interface ShardDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation ShardDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self dataHandler];
//    [self createTableView];
}

- (void)dataHandler
{
    NSString *shardStr = @"http://api2.pianke.me/timeline/info";
    // auth=&client=1&contentid=569f6ff35e7743e5188b45fb&deviceid=DB3A1424-3E92-41D9-8C86-10F84629D968&version=3.0.6
    NSDictionary *shardBodyDic = @{@"auth":@"", @"client": @"1", @"contentid": @"569f6ff35e7743e5188b45fb", @"deviceid": @"DB3A1424-3E92-41D9-8C86-10F84629D968", @"version": @"3.0.6"};
   
    [PWNetWorking POSTWithURL:shardStr body:shardBodyDic completion:^(id result) {
        self.dataArr = [ShardModel modelHanderWithArray:result[@"data"][@"list"]];
        NSLog(@"%@", result);

        [_tableView reloadData];
        
    }];

}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[ShardDetailViewController class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
}
#pragma mark - tableView 的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShardDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    // 从第一页向第二页传值 Model
    [cell.iconImg sd_setImageWithURL:[NSURL URLWithString:self.model.icon]];
    cell.unameLabel.text = self.model.uname;
    cell.timeLabel.text = self.model.addtime_f;
    // 将 Model 传值到 Cell, 进行图片比例自适应计算.
    cell.shardModel = self.model;
    if (indexPath.row == 0) {
        [cell.backImg sd_setImageWithURL:[NSURL URLWithString:self.model.coverimg]];
        cell.contentLabel.text = self.model.content;
    }
    return cell;
    
}
#pragma mark - cell 高度设置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = [ShardDetailCell heightWithModel:self.model];
    CGFloat h1 = [ShardDetailCell heithtWithText:self.model.content];
    return h + h1 + 70;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
